﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class Tab_CaseDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="CaseID">标识列ID</param>
		public bool Exists(int CaseID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [Tab_Case] where CaseID=@caseid ");
			htSyn.Add("@caseid", CaseID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<Tab_Case> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [CaseID],[CaseClassID],[CaseTitle],[CaseDescription],[CaseKey],[CaseName],[CaseImgUrl],[CaseInfo] FROM [Tab_Case]");
			 return getTab_CaseBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="CaseID">标识列ID</param>
		/// <returns></returns>
		public Tab_Case GetModel(int caseid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [CaseID],[CaseClassID],[CaseTitle],[CaseDescription],[CaseKey],[CaseName],[CaseImgUrl],[CaseInfo] FROM [Tab_Case]");
			strSql.Append(" where CaseID=@caseid ");
			htSyn.Add("@caseid", caseid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			Tab_Case tab_caseModel = new Tab_Case();
			if (dt.Rows.Count > 0)
			{
				tab_caseModel.caseClassID=Convert.ToInt32(dt.Rows[0]["CaseClassID"]);
				tab_caseModel.caseTitle=dt.Rows[0]["CaseTitle"].ToString();
				tab_caseModel.caseDescription=dt.Rows[0]["CaseDescription"].ToString();
				tab_caseModel.caseKey=dt.Rows[0]["CaseKey"].ToString();
				tab_caseModel.caseName=dt.Rows[0]["CaseName"].ToString();
				tab_caseModel.caseImgUrl=dt.Rows[0]["CaseImgUrl"].ToString();
				tab_caseModel.caseInfo=dt.Rows[0]["CaseInfo"].ToString();
			}
			return tab_caseModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="Tab_Case">实体</param>
		/// <returns></returns>
		public bool Update(Tab_Case tab_caseModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [Tab_Case] set ");
			strSql.Append("[CaseClassID]=@caseclassid,");
			strSql.Append("[CaseTitle]=@casetitle,");
			strSql.Append("[CaseDescription]=@casedescription,");
			strSql.Append("[CaseKey]=@casekey,");
			strSql.Append("[CaseName]=@casename,");
			strSql.Append("[CaseImgUrl]=@caseimgurl,");
			strSql.Append("[CaseInfo]=@caseinfo");

			strSql.Append(" where CaseID=@caseid ");
			htSyn.Add("@caseid", tab_caseModel.caseID);
			htSyn.Add("@caseclassid", tab_caseModel.caseClassID);
			htSyn.Add("@casetitle", tab_caseModel.caseTitle);
			htSyn.Add("@casedescription", tab_caseModel.caseDescription);
			htSyn.Add("@casekey", tab_caseModel.caseKey);
			htSyn.Add("@casename", tab_caseModel.caseName);
			htSyn.Add("@caseimgurl", tab_caseModel.caseImgUrl);
			htSyn.Add("@caseinfo", tab_caseModel.caseInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="CaseID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int CaseID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [Tab_Case] where CaseID=@caseid");
			htSyn.Add("caseid", CaseID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="Tab_Case">实体</param>
		/// <returns></returns>
		public bool Insert(Tab_Case tab_caseModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [Tab_Case]([CaseClassID],[CaseTitle],[CaseDescription],[CaseKey],[CaseName],[CaseImgUrl],[CaseInfo]) values (@caseclassid,@casetitle,@casedescription,@casekey,@casename,@caseimgurl,@caseinfo)");
			htSyn.Add("@caseclassid", tab_caseModel.caseClassID);
			htSyn.Add("@casetitle", tab_caseModel.caseTitle);
			htSyn.Add("@casedescription", tab_caseModel.caseDescription);
			htSyn.Add("@casekey", tab_caseModel.caseKey);
			htSyn.Add("@casename", tab_caseModel.caseName);
			htSyn.Add("@caseimgurl", tab_caseModel.caseImgUrl);
			htSyn.Add("@caseinfo", tab_caseModel.caseInfo);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<Tab_Case> getTab_CaseBySql(string strSql)
		{
			IList<Tab_Case> list = new List<Tab_Case>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					Tab_Case tab_caseModel = new Tab_Case();
					tab_caseModel.caseID= Convert.ToInt32(dr["CaseID"]);
					tab_caseModel.caseClassID= Convert.ToInt32(dr["CaseClassID"]);
					tab_caseModel.caseTitle=dr["CaseTitle"].ToString();
					tab_caseModel.caseDescription=dr["CaseDescription"].ToString();
					tab_caseModel.caseKey=dr["CaseKey"].ToString();
					tab_caseModel.caseName=dr["CaseName"].ToString();
					tab_caseModel.caseImgUrl=dr["CaseImgUrl"].ToString();
					tab_caseModel.caseInfo=dr["CaseInfo"].ToString();
					list.Add(tab_caseModel);
				}
			}
			return list;
		}
		#endregion
	}
}
